<?php
/**
 * ContentItem holds all information of given content item in database
 * @author Urban Soban
 * @package Content
 * @copyright 2009 Urban Soban
 */
class ContentItem {
	/**
	 * Article's ID in database
	 * @var int
	 */
	private $ID;
	
	/**
	 * Article's title in database
	 * @var string
	 */
	private $Title;
	
	/**
	 * Article's content in databse
	 * @var string
	 */
	private $Text;
	
	/**
	 * Database ID of article's author
	 * @var int
	 */
	private $Author;
	
	/**
	 * Holds information if article can have smarty/html tags
	 * @var boolean
	 */
	private $AllowTags;
	
	/**
	 * Holds timestamp of article's last update
	 * @var string
	 */
	private $LastUpdated = 0;
	
	/**
	 * Article flags in database
	 * @var array
	 */
	private $Flags = array();
	
	/**
	 * Instance of ContentController
	 *
	 * @var ContentController
	 */
	private $Content;
	
	/**
	 * Creates new ContentItem.
	 * First argument (ContentController) must be given, second argument is optional. Thought, if you want to load item from database,
	 * it must be given, otherwise ContentItem will not load information will not be loaded. 
	 *
	 * @param ContentController $Content
	 * @param int $ItemID
	 */
	function __construct(ContentController $Content, $ItemID = null){
		$this->Content = $Content;
		
		if($ItemID != null){
			$this->ID = $ItemID;
			
			if(!$this->loadData()){
				$this->ID = null;
			}
		}
	}
	
	/**
	 * Loads data from database.
	 * If id is not given or article with given id doesn't exist, function returns false, otherwise true.
	 *
	 * @return boolean
	 */
	function loadData(){
		if($this->ID == null){
			return false;
		}
		
		$MySQL = $this->Content->UseApps["mysql"];
		
		$SelectArticle = $MySQL->query($MySQL->generateSelectQuery($MySQL->getPrefix()."content", array("id" => $this->ID)));
		
		if(mysql_num_rows($SelectArticle) != 1){
			return false;
		}
		
		$FetchArticle = mysql_fetch_assoc($SelectArticle);
		
		$this->ID = $FetchArticle["id"];
		$this->Title = $FetchArticle["item_title"];
		$this->Text = $FetchArticle["item_text"];
		$this->Author = $FetchArticle["item_author"];
		$this->AllowTags = ($FetchArticle["item_allow_tags"] == "true") ? true : false;
		$this->LastUpdated = $FetchArticle["item_last_update"]; 
		
		$FlagsManager = $this->Content->UseApps["flags"];
		
		$this->Flags = $FlagsManager->getFlags("content_id={$this->ID}");
		
		return true;
	}
	
	/**
	 * Updates article in database.
	 * If no id was passed at object construction, this function creates new entry in database.
	 *
	 * @param string $title
	 * @param string $text
	 * @param int $author
	 * @param boolean $allow_tags
	 * @param array $flags
	 * @return boolean
	 */
	function updateItem($title, $text, $author, $allow_tags, $flags = array()){
		$MySQL = $this->Content->UseApps["mysql"];
		
		if($this->ID == null){
			$current_time = time();
			$Sql = "INSERT INTO {$MySQL->getPrefix()}content (item_title, item_text, item_author, item_allow_tags, item_last_update) VALUES ('$title', '$text', '$author', '$allow_tags', '$current_time')";
			
			$MySQL->query($Sql);
			$this->ID = mysql_insert_id();
		}
		else {
			$Sql = $MySQL->generateUpdateQuery($MySQL->getPrefix()."content", array("item_title" => $title, "item_text" => $text, "item_author" => $author, "item_allow_tags" => $allow_tags, "item_last_update" => time()), array("id" => $this->ID));
			$MySQL->query($Sql);
		}
		
		$FlagsManager = $this->Content->UseApps["flags"];
		
		$FlagsManager->updateFlags("content_id={$this->ID}", $flags);
		
		return true;
	}
	
	/**
	 * Deletes article from database
	 *
	 * @return boolean true on success, false on failure
	 */
	function deleteItem(){
		if($this->ID == null){
			return false;
		}
		
		$MySQL = $this->Content->UseApps["mysql"];
		
		$MySQL->query("DELETE FROM {$MySQL->getPrefix()}content WHERE id = {$this->ID}");
		
		return true;
	}
	
	/**
	 * Returns article ID
	 * @return int
	 */
	function getID(){
		return $this->ID;
	}
	
	/**
	 * Returns article title
	 * @return string
	 */
	function getTitle(){
		return $this->Title;
	}
	
	/**
	 * Returns article content.
	 * If {ContentItem::$AllowTags} is set to false, function escapes brackets to prevent
	 * smarty and html tags from being parsed.
	 * @return string false on failure
	 */
	function getText(){
		if($this->AllowTags == true){
			return $this->Text;
		}
		else {
			$text = htmlentities($this->Text);
			$text = str_replace(array("{", "}"), "", $text);
			
			return $text;
		}
	}
	
	/**
	 * Returns author's username
	 * On the base of {ContentItem::$Author} function returns author's username.
	 * @return string
	 */
	function getAuthor(){
		$AccountC = $this->Content->UseApps["account"];
		$AuthorAccount = $AccountC->getUserData($this->Author);
		$AuthorUsername = $AuthorAccount->getUsername();
		
		return $AuthorUsername;
	}
	
	/**
	 * Returns timestamp of last update
	 * @return string
	 */
	function getLastUpdated(){
		return $this->LastUpdated;
	}
	
	/**
	 * Function returns URL, suitable for URL.
	 * @return string
	 */
	function urlTitle(){
		$ToReplace = array("đ", "č", "ć", "š", "ž", " ");
		$ReplaceWith = array("dz", "c", "c", "s", "z", "-");
		
		$replaced_title = strtolower(str_replace($ToReplace, $ReplaceWith, $this->Title));
		$url_title = preg_replace("#[^a-z0-9-]#","", $replaced_title);
		
		return $url_title;
	}
	
	/**
	 * Function returns true if article can have HTML or smarty tags
	 * @return boolean   	
	 */	
	function getAllowTags(){
    if($this->AllowTags == true){
        return true;
    }
    return false;
  }
	
	/**
	 * Returns article flag
	 * @param string $flagName flag's name
	 * @return string
	 */
	function getFlag($flagName){
		return $this->Flags[$flagName];
	}
}

?>